package com.bm.specialvehiclefordispatcher.Tools;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import cn.jpush.android.api.JPushInterface;

import com.bm.specialvehiclefordispatcher.activity.mine.MessageAnnouncementActivity;

public class MyReceiver extends BroadcastReceiver {
	private static final String TAG = "MyReceiver";

	private NotificationManager nm;

	JSONObject jsonObject;
	String type;
	String id;

	@Override
	public void onReceive(Context context, Intent intent) {
		if (null == nm) {
			nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		}

		Bundle bundle = intent.getExtras();
		// Logger.d(TAG, "onReceive - " + intent.getAction() + ", extras: "
		// + AndroidUtil.printBundle(bundle));

		if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
			Log.i(TAG, "JPush用户注册成功");
			String title = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
			System.out.println("title" + title);

		} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
			Log.i(TAG, "接受到推送下来的自定义消息");
			String title = bundle.getString(JPushInterface.EXTRA_TITLE);
			String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
			System.out.println("自定义的title" + title + "自定义的message" + message);

		} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
			Log.i(TAG, "接受到推送下来的通知");

			receivingNotification(context, bundle);

		} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
			Log.i(TAG, "用户点击打开了通知");

			openNotification(context, bundle);

		} else {
			Log.i(TAG, "Unhandled intent - " + intent.getAction());
		}
	}

	private void receivingNotification(Context context, Bundle bundle) {
		String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
		Log.i(TAG, " title : " + title);
		String message = bundle.getString(JPushInterface.EXTRA_ALERT);
		Log.i(TAG, "message : " + message);
		String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
		Log.i(TAG, "extras : " + extras);

	}

	private void openNotification(Context context, Bundle bundle) {
		String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
		if (extras != null) {

			try {
				jsonObject = new JSONObject(extras);
				type = jsonObject.getString("type");
				//id = jsonObject.getString("id");
				// ToastMgr.show(type+"-" + id);
				
				System.out.println("type" + type);
				System.out.println("id" + id);

				Intent intent = new Intent();
				switch (Integer.parseInt(type)) {
				// 消息
				case 0:
				case 1:
					// intent.setClass(context, OrderDetailActivity.class);
					// intent.putExtra("ORDER_ID", Long.valueOf(id));
					intent.setClass(context, MessageAnnouncementActivity.class);
					break;
				case 2:
					intent.setClass(context, MessageAnnouncementActivity.class);
					intent.putExtra("type", 1);
					break;

				default:
					break;
				}
				
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
				context.startActivity(intent);
			} catch (JSONException e) {
				e.printStackTrace();
				return;
			}
		}
		// Intent i = new Intent(context, MessageAnnouncementActivity.class);
		// i.putExtra("startType", 2);
		// // i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
		// | Intent.FLAG_ACTIVITY_CLEAR_TOP);
		// context.startActivity(i);
		//
		// context.startActivity(new Intent(context,
		// .class));
		// try {
		// JSONObject extrasJson = new JSONObject(extras);
		// myValue = extrasJson.optString("myKey");
		// } catch (Exception e) {
		// Log.i(TAG, "Unexpected: extras is not a valid json", e);
		// return;
		// }
		// if (TYPE_THIS.equals(myValue)) {
		// Intent mIntent = new Intent(context, ThisActivity.class);
		// mIntent.putExtras(bundle);
		// mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		// context.startActivity(mIntent);
		// } else if (TYPE_ANOTHER.equals(myValue)) {
		// Intent mIntent = new Intent(context, AnotherActivity.class);
		// mIntent.putExtras(bundle);
		// mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		// context.startActivity(mIntent);
		// }
	}
}